package com.corner.model.cornerservice.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class CodeGenerator {
    String beans[] = {"Info", "Location", "Conversation", "GeoPoint", "Like",
            "ReportUser", "ReportInfo", "Attention"};

    public void serviceGenerator() {
        BufferedReader br = null;
        try {
            InputStream in = new FileInputStream(
                    "src/com/corner/client/service/UserService.java");
            br = new BufferedReader(new InputStreamReader(in));

            String str;
            StringBuilder sb = new StringBuilder();
            while ((str = br.readLine()) != null) {
                sb.append(str).append("\n");
            }

            for (String clazz : beans) {
                FileWriter fileWriter = new FileWriter(
                        "src/com/corner/client/service/UserService.java"
                                .replaceAll("User", clazz));

                String firstChar = clazz.substring(0, 1);
                String resourceName = clazz.replaceFirst(firstChar,
                        firstChar.toLowerCase());
                fileWriter.write(sb.toString().replaceAll("User", clazz)
                        .replaceAll("user", resourceName));

                fileWriter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void generateCreateService() {
        String str = "public UserService userService = adapter.create(UserService.class);";
        for (String clazz : beans) {
            String firstChar = clazz.substring(0, 1);
            String resourceName = clazz.replaceFirst(firstChar,
                    firstChar.toLowerCase());
            System.out.println(str.replaceAll("User", clazz).replaceAll("user",
                    resourceName));
        }
    }
}
